package com.leyou.item.controller;

import com.leyou.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：qcq
 * @date ：Created in 2020/12/5 9:19
 * @description：
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/page")
    public ResponseEntity<PageDTO> findPage(@RequestParam(value = "key", required = false) String key, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "rows", required = false) Integer rows) {
        return ResponseEntity.ok(brandService.findPage(key, page, rows));
    }

    @PostMapping
    public ResponseEntity saveBrand(BrandDTO brandDto) {
        brandService.saveBrand(brandDto);
        return  ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity deleteBrandById(@PathVariable("id") Long id) {
        brandService.deleteBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping
    public ResponseEntity updateBrand(BrandDTO brandDto /*, BindingResult bindingResult不加会报错，TODO*/) {
        brandService.updateBrand(brandDto);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandListByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(brandService.queryBrandListByIds(ids));
    }

    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(brandService.queryBrandById(id));
    }

    /*todo*/
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(brandService.queryBrandByCategoryId(id));
    }

}
